window.onload=function ()
{
    var vue = new Vue({
        el:"#app",
        data:{
            brandList:[],
            brand:{},
            pages:0,
            current:1,
            searchBrand:{},
            ids:[],
            checked:false
        },
        methods:{
            findByPage:function (page)
            {
                //查找或换页的时候将之前选中的ids清除，取消全选
                this.ids=[];
                //console.log("hah");
                //console.log(page);
                axios.get("/brand/findByPage?page="+page,{params:this.searchBrand}).then(function (res)
                {
                    //console.log(this);
                    //res的json={pages=100,rows=[{},{}]}
                    vue.brandList=res.data.rows;
                    vue.pages=res.data.pages;
                    vue.current=page;
                    //console.log(vue.brandList);
                });
            },
            saveOrUpdate:function ()
            {
                //console.log(this.brand);
                var url="save";
                if(this.brand.id)
                {
                    url="update";
                }
                axios.post("/brand/"+url,vue.brand).then(function (res)
                {
                    if(res.data)
                    {
                        vue.findByPage(vue.current);
                    }
                    else
                    {
                        alert("请不要瞎搞！");
                    }
                });
            },
            show:function (brand)
            {
                //把原来的brand转化为JSON字符串
                var brandJson = JSON.stringify(brand);
                //再把JSON字符串解析为新的对象
                this.brand=JSON.parse(brandJson);
            },
            checkAll:function (event)
            {
                //防止在ids中重复选择id，每次点击清除上次选中的id
                this.ids=[];
                //alert(event.target.checked);
                //选中的话选中所有的记录
                if(event.target.checked)
                {
                    for (var i=0;i<this.brandList.length;i++)
                    {
                        this.ids.push(this.brandList[i].id);
                    }
                }
            },
            del:function ()
            {
                if(this.ids.length > 0)
                {
                    axios.get("/brand/delete?ids="+this.ids).then(function (response)
                    {
                        if(response)
                        {
                            vue.current=((vue.checked && vue.current==vue.pages)?vue.current-1:vue.current);
                            if(vue.current<0)
                            {
                                vue.current=1;
                            }
                            vue.findByPage(vue.current);
                        }
                        else
                        {
                            alert("删除失败！");
                        }
                    })
                }
                else
                {
                    alert("请选择需要删除的记录");
                }
            }
        },
        created:function ()
        {
            this.findByPage(this.current);
        },
        updated:function ()
        {
            //监听子选项的选择状态，与父选项的选择状态同步
            this.checked=(this.ids.length==this.brandList.length);
        }

    });
};